//
//  HomeTestingTableViewCell.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/16.
//

#import "HomeTestingTableViewCell.h"
#import "HomeQustionInfoViewController.h"
#import "JMScanningQRCodeUtils.h"
#import "HomeQuestionEcodeDeviceViewController.h"
#import "HomeAvailableNetworking.h"
#import "SaTime.h"



@interface HomeTestingTableViewCell ()

@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UILabel * downTimeLabel;
@property (nonatomic,strong) UILabel * typeLabel;
@property (nonatomic,strong) UILabel * addLabel;
@property (nonatomic,strong) UILabel * timeLabel;
@property (nonatomic,strong) UILabel * durationLabel;
@property (nonatomic,strong) UIView * v;
@property (nonatomic,strong) UIButton * testBtn;

@property (nonatomic,strong) UIButton * qiandaoBtn;
@property (nonatomic,strong) UIButton * jinruBtn;




@property (nonatomic,strong) HomeCanleTestNetworking * manager;
@property (nonatomic,strong) HomeCheckInNetWorking * checkInManager;



@end



@implementation HomeTestingTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        
        self.contentView.backgroundColor = COLOR_RGB(241, 241, 241);
        [self configUI];
        
    }
    return self;
}





-(void)setModel:(HomeAvailableModel *)model{
    _model = model;
    self.titleLabel.text = model.subject;
    self.typeLabel.text = model.subject;
    
    // 默认隐藏所有按钮
    self.testBtn.hidden = YES;
    self.qiandaoBtn.hidden = YES;
    self.jinruBtn.hidden = YES;
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 将签到开始时间转换为NSDate
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *checkinStartDate = [formatter dateFromString:model.checkinStartTime];
    
    // 计算时间差（单位：分钟）
    NSTimeInterval timeInterval = [checkinStartDate timeIntervalSinceDate:currentDate];
    double minutesDifference = timeInterval / 60;
    
    if ([model.examStatus intValue] == 1) { // 可以预约
        self.testBtn.hidden = NO; // 显示取消预约按钮
        self.downTimeLabel.text = @"已预约";
        
        // 如果距离签到开始时间小于等于10分钟
        if (minutesDifference <= 10 && minutesDifference > 0) {
            self.qiandaoBtn.hidden = NO; // 显示开始签到按钮
            self.downTimeLabel.text = [NSString stringWithFormat:@"距签到开始%.0f分钟", minutesDifference];
        }
    } else if ([model.examStatus intValue] == 2) { // 已签到
        self.jinruBtn.hidden = NO; // 显示开始考试按钮
        self.downTimeLabel.text = @"已签到";
    }
    
    /*   整理数据  **/
    
    self.addLabel.text = [NSString stringWithFormat:@"考试地点：%@",model.location];
    self.timeLabel.text = [NSString stringWithFormat:@"考试时间：%@",model.startTime];
    self.durationLabel.text = [NSString stringWithFormat:@"考试时长：%@",[SaTime transFormTime:model.startTime endStr:model.endTime]];
    
}




-(void)configUI{
    
    [self.contentView addSubview:self.follewView];
    [self.follewView addSubview:self.titleLabel];
    [self.follewView addSubview:self.downTimeLabel];
    [self.follewView addSubview:self.typeLabel];
    [self.follewView addSubview:self.addLabel];
    [self.follewView addSubview:self.timeLabel];
    [self.follewView addSubview:self.durationLabel];
    [self.follewView addSubview:self.v];
    [self.follewView addSubview:self.testBtn];
    [self.follewView addSubview:self.qiandaoBtn];
    [self.follewView addSubview:self.jinruBtn];
    
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.contentView).with.offset(ALIGN_HEIGHT_PT(10));
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(10));
        make.top.equalTo(self.follewView).with.offset(ALIGN_HEIGHT_PT(12));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(10));
    }];
    
    [self.downTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(10));
        make.centerY.equalTo(self.titleLabel.mas_centerY);
    }];
    
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    [self.addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.typeLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(15));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.addLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    [self.v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.durationLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(15));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(12));
        make.height.equalTo(@(1));
    }];
    
    
    [self.testBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.v.mas_bottom).with.offset(ALIGN_HEIGHT_PT(12));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(80), ALIGN_HEIGHT_PT(30))));
    }];
    
    
    [self.qiandaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.testBtn.mas_left).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.v.mas_bottom).with.offset(ALIGN_HEIGHT_PT(12));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(80), ALIGN_HEIGHT_PT(30))));
    }];
    
    [self.jinruBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.qiandaoBtn.mas_left).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.v.mas_bottom).with.offset(ALIGN_HEIGHT_PT(12));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(80), ALIGN_HEIGHT_PT(30))));
    }];
    
    
    
    
}




#pragma mark -- 点击事件
-(void)respondsTo_testBtn{  // 取消预约
    
    [LUtilityTools showAlertWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"是否取消预约%@",self.model.subject] cancelTitle:@"不了" buttonAction:^{
        
    } sureTitle:@"确认取消" ButtonAction:^{
        
        NSDictionary * paramer = @{@"examId":self.model.id};
        [self.manager dispatchTaskWithParam:paramer completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (responseObject && [responseObject[@"code"] intValue] ==  200){
                [MBProgressHUD showSuccess:@"取消成功"];
                if (self.delegate && [self.delegate respondsToSelector:@selector(realodaTestingTableView)]){
                    [self.delegate realodaTestingTableView];
                }
                
            }else{
                [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                    
                }];
            }
        }];
        
    }];
    

 
    
}


-(void)respondsTo_qiandaoBtn{ // 开始签到
    
    [LUtilityTools showAlertWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"是否开始签到%@",self.model.subject] cancelTitle:@"不了" buttonAction:^{
        
    } sureTitle:@"开始签到" ButtonAction:^{
        
        
        
        [JMScanningQRCodeUtils jm_cameraAuthStatusWithSuccess:^{
              
            HomeQuestionEcodeDeviceViewController * vc = [HomeQuestionEcodeDeviceViewController new];
            vc.block = ^(NSString * str) {
                
                NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
                NSError *error = nil;
                id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
                 
                if (error) {
                    NSLog(@"Error parsing JSON: %@", error);
                } else {
                    NSDictionary *dictionary = (NSDictionary *)result;
                    
                    NSDictionary * paramer = @{@"examId":dictionary[@"examId"]};
                    [self.checkInManager dispatchTaskWithParam:paramer completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                        if (responseObject && [responseObject[@"code"] intValue] ==  200){
                            [MBProgressHUD showSuccess:@"签到成功"];
                        }else{
                            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{

                            }];
                        }
                    }];
                    
                    
                    
                    // 使用字典
                }
                
            };
            [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
            
            } failure:^{
                
            }];
        
        
        
        
        
        
        
        
        
       
        
    }];
}


-(void)respondsTo_jinruBtn{
    HomeQustionInfoViewController * vc = [HomeQustionInfoViewController new];
    vc.examId = self.model.id;
    [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    
}



#pragma mark -- 懒加载
-(UIView *)follewView{
    if (!_follewView){
        _follewView = [UIView new];
        _follewView.backgroundColor = [UIColor whiteColor];
        _follewView.layer.cornerRadius = 10;
        _follewView.layer.masksToBounds = YES;
    }
    return _follewView;
}

-(UILabel *)titleLabel{
    if (!_titleLabel){
        _titleLabel = [UILabel new];
        _titleLabel.text = @"长期人员安全考试";
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:0.5];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}


-(UILabel *)downTimeLabel{
    if (!_downTimeLabel){
        _downTimeLabel = [UILabel new];
        _downTimeLabel.text = @"长期人员安全考试";
        _downTimeLabel.textColor = [UIColor blackColor];
        _downTimeLabel.font = FONT14;
        _downTimeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _downTimeLabel;
}



-(UILabel *)typeLabel{
    if (!_typeLabel){
        _typeLabel = [UILabel new];
        _typeLabel.text = @"申办长期人员通行证";
        _typeLabel.textColor = [UIColor blackColor];
        _typeLabel.font = FONT14;
        _typeLabel.textAlignment = NSTextAlignmentLeft;
        _typeLabel.backgroundColor = COLOR_RGB(244, 244, 244);
    }
    return _typeLabel;
}

-(UILabel *)addLabel{
    if (!_addLabel){
        _addLabel = [UILabel new];
        _addLabel.text = @"考试地点：机场楼13楼01号";
        _addLabel.textColor = [UIColor blackColor];
        _addLabel.textAlignment = NSTextAlignmentLeft;
        _addLabel.font = FONT14;
    }
    return _addLabel;
}

-(UILabel *)timeLabel{
    if (!_timeLabel){
        _timeLabel = [UILabel new];
        _timeLabel.text = @"考试时间：2024.11.05 10:30";
        _timeLabel.textColor = [UIColor blackColor];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.font = FONT14;
    }
    return _timeLabel;
}
-(UILabel *)durationLabel{
    if (!_durationLabel){
        _durationLabel = [UILabel new];
        _durationLabel.text = @"考试时长：60分钟";
        _durationLabel.textColor = [UIColor blackColor];
        _durationLabel.textAlignment = NSTextAlignmentLeft;
        _durationLabel.font = FONT14;
    }
    return _durationLabel;
}

-(UIView *)v{
    if (!_v){
        _v = [UIView new];
        _v.backgroundColor = COLOR_RGB(228, 228, 228);
    }
    return _v;
}

-(UIButton *)testBtn{
    if (!_testBtn){
        _testBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_testBtn setTitle:@"取消预约" forState:(UIControlStateNormal)];
        [_testBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _testBtn.titleLabel.font = FONT14;
        _testBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        _testBtn.layer.cornerRadius = 5;
        _testBtn.layer.masksToBounds = YES;
        [_testBtn addTarget:self action:@selector(respondsTo_testBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _testBtn;
}



-(UIButton *)qiandaoBtn{
    if (!_qiandaoBtn){
        _qiandaoBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_qiandaoBtn setTitle:@"开始签到" forState:(UIControlStateNormal)];
        [_qiandaoBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _qiandaoBtn.titleLabel.font = FONT14;
        _qiandaoBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        _qiandaoBtn.layer.cornerRadius = 5;
        _qiandaoBtn.layer.masksToBounds = YES;
        [_qiandaoBtn addTarget:self action:@selector(respondsTo_qiandaoBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _qiandaoBtn;
}


-(UIButton *)jinruBtn{
    if (!_jinruBtn){
        _jinruBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_jinruBtn setTitle:@"开始考试" forState:(UIControlStateNormal)];
        [_jinruBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _jinruBtn.titleLabel.font = FONT14;
        _jinruBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        _jinruBtn.layer.cornerRadius = 5;
        _jinruBtn.layer.masksToBounds = YES;
        [_jinruBtn addTarget:self action:@selector(respondsTo_jinruBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _jinruBtn;
}




-(HomeCanleTestNetworking *)manager{
    if (!_manager){
        _manager = [HomeCanleTestNetworking new];
    }
    return _manager;
}

-(HomeCheckInNetWorking *)checkInManager{
    if (!_checkInManager){
        _checkInManager = [HomeCheckInNetWorking new];
    }
    return _checkInManager;
}




@end
